package com.application.lucky.activity.me;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.bean.jsonBean.RechargeItemBean;
import com.application.lucky.request.CallBackEx;
import com.application.lucky.request.RedPRequest;
import com.application.lucky.widget.TitlebarView;
import com.application.mylibrary.base.BaseActivity;
import com.application.mylibrary.utils.LogUtils;
import com.application.mylibrary.utils.ToastUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;
import com.qunar.im.ui.view.recyclerview.BaseQuickAdapter;
import com.qunar.im.ui.view.recyclerview.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

public class ToRechargeActivity extends BaseActivity {

    private TitlebarView mTitleBar;
    private TextView mTvRechargeChannelName;
    private RecyclerView mRclRechargeList;
    private TextView mTtRechargeNum;
    private Button mTvRechargeButton;
    private MyAdapter adapter;
    private String channel = "";
    private String channel_name = "";
    private String card = "";
    private String card_channel = "";
    private TextView tv_1;
    private TextView iv_2_bak;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }


    @Override
    public void beforeInit() {
        Intent intent = getIntent();
        if (intent != null) {
            channel = intent.getStringExtra("channel");
            channel_name = intent.getStringExtra("channel_name");
            card = intent.getStringExtra("card");
            card_channel = intent.getStringExtra("card_channel");
            LogUtils.i("channel===" + channel);
            LogUtils.i("channel_name===" + channel_name);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_to_recharge;
    }

    private void initView() {
        mTitleBar = findViewById(R.id.title_bar);
        mTitleBar.setTitle(channel + "支付");
        mTitleBar.setOnViewClick(new TitlebarView.onViewClick() {
            @Override
            public void leftClick() {
                finishAnim();
            }

            @Override
            public void rightClick() {

            }
        });
        mTvRechargeChannelName = findViewById(R.id.tv_recharge_channel_name);
        mTvRechargeChannelName.setText(channel_name);
        mTvRechargeChannelName.setVisibility(View.GONE);
        mRclRechargeList = findViewById(R.id.rcl_recharge_list);
        mTtRechargeNum = findViewById(R.id.tt_recharge_num);
        mTvRechargeButton = findViewById(R.id.tv_recharge_button);
        tv_1 = findViewById(R.id.tv_1);
        tv_1.setText(channel);
        iv_2_bak = findViewById(R.id.iv_2_bak);
        iv_2_bak.setText(card);
        mRclRechargeList.setLayoutManager(new GridLayoutManager(this, 3));
        adapter = new MyAdapter(R.layout.item_recharge_channel_nul);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MyAdapter myAdapter = ToRechargeActivity.this.adapter;
                RechargeItemBean item = myAdapter.getItem(position);
                if (item != null) {
                    mTtRechargeNum.setText(item.num);
                }

                myAdapter
                        .setSelectPosition(position);
            }
        });
        mRclRechargeList.setAdapter(adapter);
        loadData();
        mTvRechargeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int selectPosition = adapter.getSelectPosition();
                if (selectPosition >= 0) {
                    final RechargeItemBean item = adapter.getItem(selectPosition);
                    if (item != null) {
                        int moneyNum = Integer.parseInt(item.num);

                        // 微信 1 ，支付宝 2 ，银行卡 3
                        new RedPRequest().red_recharge_submit(String.valueOf(moneyNum * 100), card_channel, new CallBackEx<BaseResponse>() {
                            @Override
                            public void onFailed(int code) {
                                ToastUtils.show("充值了失败");
                            }

                            @Override
                            public void onSuccess(BaseResponse baseResponse) {
                                ToastUtils.show("处理中，请在30分钟内完成转账");
                            }
                        });
                    }
                } else {
                    ToastUtils.show("请选择金额");
                }

            }
        });

        findViewById(R.id.btn_copy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData clipData = ClipData.newPlainText("Label", iv_2_bak.getText().toString());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(clipData);
                ToastUtils.show("复制账号成功");
            }
        });
    }

    private void loadData() {
        List<RechargeItemBean> datas = new ArrayList<>();
        RechargeItemBean rechargeItemBean = new RechargeItemBean();
        rechargeItemBean.num = "100";
        datas.add(rechargeItemBean);
        rechargeItemBean = new RechargeItemBean();
        rechargeItemBean.num = "500";
        datas.add(rechargeItemBean);
        rechargeItemBean = new RechargeItemBean();
        rechargeItemBean.num = "1000";
        datas.add(rechargeItemBean);
        rechargeItemBean = new RechargeItemBean();
        rechargeItemBean.num = "2000";
        datas.add(rechargeItemBean);
        rechargeItemBean = new RechargeItemBean();
        rechargeItemBean.num = "5000";
        datas.add(rechargeItemBean);
        rechargeItemBean = new RechargeItemBean();
        rechargeItemBean.num = "10000";
        datas.add(rechargeItemBean);
        adapter.setNewData(datas);
    }

    static class MyAdapter extends BaseQuickAdapter<RechargeItemBean, BaseViewHolder> {
        private int selectPosition = -1;

        public MyAdapter(int layoutResId) {
            super(layoutResId);
        }

        public int getSelectPosition() {
            return selectPosition;
        }

        public void setSelectPosition(int selectPosition) {
            this.selectPosition = selectPosition;
            notifyDataSetChanged();
        }

        @Override
        protected void convert(BaseViewHolder helper, RechargeItemBean item) {
            int position = helper.getAdapterPosition();

            helper.setBackgroundRes(R.id.rb_recharge_item, position == selectPosition ? R.drawable.shape_recharge_channel : R.drawable.shape_recharge_channel_un)
                    .setText(R.id.rb_recharge_item, item.num);
        }
    }
}
